package com.moonlight.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moonlight.service.ActCardResultService;

/**
 * 
 * @ClassName ActCardResultController
 * @Description 测试接口类
 * @author wuxiangyi
 * @date 2022年9月4日 下午6:32:52
 */
@RestController
@RequestMapping(value = "/actCard")
public class ActCardResultController {
	
	private static final Logger log = LoggerFactory.getLogger(ActCardResultController.class);
	
	@Autowired
	private ActCardResultService actCardResultService;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@RequestMapping(value = "getParsingSql")
	public JSONObject getParsingSql(HttpServletRequest request) {
		BufferedReader reader;
		JSONObject response = new JSONObject();
		StringBuffer buffer = new StringBuffer();
		try {
			reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
			String s = reader.readLine();
	        JSONObject requestObject = JSONObject.parseObject(s);
	        JSONObject data = requestObject.getJSONObject("data");
	        String condition = data.getString("condition");
	        JSONArray ruleArray = data.getJSONArray("rule");
	        JSONArray groupArray = data.getJSONArray("group");
	        this.actCardResultService.doParse(condition, ruleArray, groupArray, buffer);
	        log.info(buffer.toString());
	        List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from act_card_result where " + buffer.toString());
	        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
	        JSONArray resultArray = new JSONArray();
	        for (int i = 0 ; i < result.size() ; i ++) {
	        	Map<String , Object> map = result.get(i);
	        	int id = Integer.parseInt(map.get("id").toString());
	        	String time = sdf.format(sdf.parse(map.get("create_time").toString()));
	        	String name = map.get("name").toString();
	        	int type = Integer.parseInt(map.get("type").toString());
	        	String user_id = map.get("user_id").toString();
	        	JSONObject tempObject = new JSONObject();
	        	tempObject.put("id" , id);
	        	tempObject.put("time" , time);
	        	tempObject.put("name" , name);
	        	tempObject.put("type" , type);
	        	tempObject.put("user_id" , user_id);
	        	resultArray.add(tempObject);
	        }
	        JSONObject dataObject = new JSONObject();
        	dataObject.put("sql" , buffer.toString());
        	dataObject.put("array" , resultArray);
    		response.put("data" , dataObject);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.put("res" , false);
			response.put("message" , "条件格式有误");
		}
		response.put("res" , true);
		return response;
	}
}
